/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	ImageLinkModifier, Member of System72.NTextile 
/// Description      :	ImageLinkModifier description
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System;
using System.Text.RegularExpressions;
using System72.NTextile.RegexLibrary;

namespace System72.NTextile
{
    internal class ImageLinkModifier : IModifier
    {
        #region Private Variables

        private readonly Operation m_operation = new Operation();
        private AttributeCollection m_supportedAttributes;
        private PunctuationCollection m_supportedPunctuations;
        private bool m_success;
        private readonly int m_parseOrder=6;
        private readonly ModifierType m_type;
        
        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        public ImageLinkModifier()
        {
            m_type = ModifierType.ImageLink;
        }

        #endregion
        
        #region Public Properties
        /// <summary>
        /// 
        /// </summary>
        public AttributeCollection SupportedAttributes
        {
            get
            {
                return m_supportedAttributes;
            }
            set
            {
                m_supportedAttributes = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public PunctuationCollection SupportedPunctuations
        {
            get { return m_supportedPunctuations; }
            set { m_supportedPunctuations = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int ParseOrder
        {
            get { return m_parseOrder; }
        }
        /// <summary>
        /// 
        /// </summary>
        public ModifierType Type
        {
            get { return m_type; }
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contentText"></param>
        /// <param name="excludedModifiers"></param>
        /// <returns></returns>
        public string GetXHTML(string contentText, ModifierCollection excludedModifiers)
        {
            if (m_operation != null)
            {
                if (!excludedModifiers.ContainsModifier(this))
                {
                    ImageLinkRegex imageLinkRegex = new ImageLinkRegex();
                    MatchCollection matchCollection = imageLinkRegex.GetMatches(contentText);
                    foreach (Match match in matchCollection)
                    {
                        //Since there is atleast one match then we can set Success to true
                        m_success = true;
                        string matchText;
                        string imageSource = match.Groups[1].Value;
                        string altText = match.Groups[2].Value;
                        string href = match.Groups[3].Value;
                        altText = altText.Replace(altText.Substring(0, 1), String.Empty);//Remove starting parenthesis
                        altText = altText.Replace(altText.Substring(altText.Length - 1, 1), String.Empty);//Remove ending parenthesis
                        string imageAttributeText = "src=\"" + imageSource + "\" " + "alt=\"" + altText + "\"";
                        string imageMarkUp = Util.GetSelfClosingElementMarkUp("img", imageAttributeText);
                        string attributeText = String.Format(" href={0}{1}{0} ", Util.DoubleQuote, href);
                        matchText = Util.GetElementMarkUp(imageMarkUp, "a", attributeText);
                        //Do the HTML only if any content is present between the operators
                        if (match.Value.Length > 0)
                        {
                            contentText = contentText.Replace(match.Value, matchText);
                        }
                        //No attributes are processed as ImageLink Modifier doesn't support one.
                    }
                }
            }
            return contentText;
        }

        #endregion

    }
}
